/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.widget;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.ColorUtils;

/**
 * 颜色选择器
 *
 * @author xuexiang
 * @since 2018/12/28 上午10:15
 */
public class SelectorColorDialog extends CommonDialog {

    private OnColorSelectedListener mOnColorSelectedListener;

    /**
     * 构造窗体
     *
     * @param context
     * @param listener
     */
    public SelectorColorDialog(Context context, OnColorSelectedListener listener) {
        super(context);
        mOnColorSelectedListener = listener;
        setAutoClosable(true);
        setAlignment(LayoutAlignment.CENTER);
        setContentCustomComponent(getContentView(context));
    }



    private Component getContentView(final Context context) {
        ListContainer gv = new ListContainer(context);
        TableLayoutManager tableLayout = new TableLayoutManager();
        tableLayout.setColumnCount(5);

        tableLayout.setOrientation(Component.HORIZONTAL);
        gv.setLayoutManager(tableLayout);

        gv.setItemProvider(new BaseItemProvider() {
            int[] colors = new int[]{0xffffffff, 0xff000000, 0xffe51c23, 0xffE84E40, 0xff9c27b0, 0xff673ab7,
                0xff3f51b5, 0xff5677fc, 0xff03a9f4, 0xff00bcd4, 0xff009688, 0xff259b24, 0xff8bc34a, 0xffcddc39,
                0xffffeb3b, 0xffffc107, 0xffff9800, 0xffff5722, 0xff795548};

            @Override
            public int getCount() {
                return colors.length;
            }

            @Override
            public Object getItem(int i) {
                return colors[i];
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
                Component v = null;
                if (component == null) {
                    v = new Component(context);
                    v.setBackground(ColorUtils.getColorShapeElement(colors[position]));
                    v.setClickedListener(v1 -> {
                        if (mOnColorSelectedListener != null) {
                            mOnColorSelectedListener.onColorSelected(colors[position]);
                        }
                        hide();

                    });
                    ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig();
                    layoutConfig.width = (int) (DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().width / 5f);
                    layoutConfig.height = (int) (DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().width / 5f);
                    v.setLayoutConfig(layoutConfig);
                } else {
                    v = component;
                }
                return v;
            }
        });
        return gv;
    }


    /**
     * 颜色选择
     */
    public interface OnColorSelectedListener {
        /**
         * 颜色选择
         *
         * @param color
         */
        void onColorSelected(int color);
    }
}
